package com.kmist.xiakexing.data;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.kmist.xiakexing.tools.Config;
import com.kmist.xiakexing.utils.scene.PlayerBean;

public class PlayerDao implements IPlayerDao{
    private static final String TAG = "PlayerDao";
    private static PlayerDao sInstance;
    private IPlayerDaoCallback mCallback;
    private XiakexingDBHelper xiakexingDBHelper;
    private SQLiteDatabase db;
    private Cursor c;

    public static PlayerDao getsInstance() {
        if (sInstance == null) {
            synchronized (PlayerDao.class){
                sInstance = new PlayerDao();
            }
        }
        return sInstance;
    }

    public PlayerDao() {
        xiakexingDBHelper = new XiakexingDBHelper(Config.context);
    }

    @Override
    public void setCallback(IPlayerDaoCallback callback) {
        this.mCallback = callback;
    }

    @Override
    public void addPlayer(PlayerBean playerBean) {
        try {
            Log.d(TAG,playerBean.toString());
            db = xiakexingDBHelper.getWritableDatabase();
            String sql = "insert into game(token,sid,uname,ulv,uyxb,uexp,uhp,umaxhp,ugj,ufy,uwx,usex,vip,nowmid,endtime,sfzx) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            db.execSQL(sql,new Object[]{"123456", playerBean.getSid(),playerBean.getUname(), playerBean.getUlv(), playerBean.getUyxb(), playerBean.getUexp(),
                        playerBean.getUhp(), playerBean.getUmaxhp(), playerBean.getUgj(), playerBean.getUfy(), playerBean.getUwx(), playerBean.getUsex(), playerBean.getVip(),
                        playerBean.getNowmid(), playerBean.getEndtime(), playerBean.getSfzx()});

            if (mCallback != null) {
                mCallback.onPlayerAdded();
            }
        } catch (Exception e) {
            if (mCallback != null) {
                mCallback.onPlayerAddError();
            }
            e.printStackTrace();
        }finally {
            db.close();
        }
    }

    @Override
    public void delPlayer(PlayerBean playerBean) {
        try {
            db = xiakexingDBHelper.getWritableDatabase();
            String sql = "";
            db.execSQL(sql);
            if (mCallback != null) {
                mCallback.onPlayerDeled();
            }
        } catch (Exception e) {
            if (mCallback != null) {
                mCallback.onPlayerDelError();
            }
            e.printStackTrace();
        }finally {
            db.close();
        }
    }

    @Override
    public void updatePlayer(PlayerBean playerBean) {
        try {
            db = xiakexingDBHelper.getWritableDatabase();
            String sql = "";
            db.execSQL(sql);
            if (mCallback == null) {
                mCallback.onPlayerUpdateOK();
            }
        } catch (Exception e) {
            if (mCallback == null) {
                mCallback.onPlayerUpdateErr();
            }
            e.printStackTrace();
        }finally {
            db.close();
        }
    }

    @Override
    public PlayerBean getPlayer(String sid) {
        PlayerBean playerBean = new PlayerBean();
        try {
            db = xiakexingDBHelper.getReadableDatabase();
            c = db.query(Config.TABLE_NAME_GAME, null, "sid=?", new String[]{sid}, null, null,null);
            if(c.moveToFirst()){
                do{
                    playerBean.setUname(c.getString(c.getColumnIndex("uname")));
                    playerBean.setSid(c.getString(c.getColumnIndex("sid")));
                    playerBean.setUid(c.getString(c.getColumnIndex("uid")));
                    playerBean.setUlv(c.getInt(c.getColumnIndex("ulv")));
                    playerBean.setUyxb(c.getInt(c.getColumnIndex("uyxb")));
                    playerBean.setUexp(c.getInt(c.getColumnIndex("uexp")));
                    playerBean.setUhp(c.getInt(c.getColumnIndex("uhp")));
                    playerBean.setUmaxhp(c.getInt(c.getColumnIndex("umaxhp")));
                    playerBean.setUgj(c.getInt(c.getColumnIndex("ugj")));
                    playerBean.setUfy(c.getInt(c.getColumnIndex("ufy")));
                    playerBean.setUbj(c.getInt(c.getColumnIndex("ubj")));
                    playerBean.setUxx(c.getInt(c.getColumnIndex("uxx")));
                    playerBean.setUwx(c.getInt(c.getColumnIndex("uwx")));
                    playerBean.setUsex(c.getString(c.getColumnIndex("usex")));
                    playerBean.setVip(c.getInt(c.getColumnIndex("vip")));
                    playerBean.setNowmid(c.getInt(c.getColumnIndex("nowmid")));
                    playerBean.setEndtime(c.getString(c.getColumnIndex("endtime")));
                    playerBean.setTool1(c.getInt(c.getColumnIndex("tool1")));
                    playerBean.setTool2(c.getInt(c.getColumnIndex("tool2")));
                    playerBean.setTool3(c.getInt(c.getColumnIndex("tool3")));
                    playerBean.setTool4(c.getInt(c.getColumnIndex("tool4")));
                    playerBean.setTool5(c.getInt(c.getColumnIndex("tool5")));
                    playerBean.setTool6(c.getInt(c.getColumnIndex("tool6")));
                    playerBean.setSfzx(c.getInt(c.getColumnIndex("sfzx")));
                    playerBean.setSfxl(c.getInt(c.getColumnIndex("sfxx")));
                    playerBean.setYp1(c.getInt(c.getColumnIndex("yp1")));
                    playerBean.setYp2(c.getInt(c.getColumnIndex("yp2")));
                    playerBean.setYp3(c.getInt(c.getColumnIndex("yp3")));
                    playerBean.setJn1(c.getInt(c.getColumnIndex("jn1")));
                    playerBean.setJn2(c.getInt(c.getColumnIndex("jn2")));
                    playerBean.setJn3(c.getInt(c.getColumnIndex("jn3")));
                    playerBean.setCw(c.getInt(c.getColumnIndex("cw")));
                    playerBean.setIspvp(c.getInt(c.getColumnIndex("ispvp")));
                }while (c.moveToNext());
                if (mCallback != null) {
                    mCallback.onPlayerLoaded(playerBean);
                }
                return playerBean;
            }else{
                if(mCallback != null){
                    mCallback.onPlayerLoadEmpty();
                }
            }
        } catch (Exception e) {
            if(mCallback != null){
                mCallback.onPlayerLoadErr();
            }
            e.printStackTrace();
        }finally {
            c.close();
            db.close();
        }
        return null;
    }

    @Override
    public void changeExp(String sid, int exp) {
        try {
            db = xiakexingDBHelper.getWritableDatabase();
            String sql = "";
            db.execSQL(sql);
            if (mCallback == null) {
                mCallback.onPlayerUpdateOK();
            }
        } catch (Exception e) {
            if (mCallback == null) {
                mCallback.onPlayerUpdateErr();
            }
            e.printStackTrace();
        }finally {
            db.close();
        }
    }

    @Override
    public void updatePlayerSx(String mibName, String mibValue, String sid) {
        try {
            db = xiakexingDBHelper.getWritableDatabase();
            String sql = "update game set "+ mibName+ " = ? WHERE sid = ?";
            db.execSQL(sql, new Object[]{mibValue, sid});
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            db.close();
        }
    }
}
